package com.wsd.jing.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsd.jing.entity.Test;
import com.wsd.jing.mapper.TestMapper;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.service.ITestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsd.jing.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.service.impl
 * @className: TestServiceImpl
 * @description: Test服务实现类
 * @createDate 2024-07-04 03:57
 * @copyright 2024-07-04 03:57 www.tom.com Inc. All rights reserved.
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements ITestService {

    private final TestMapper testMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertTest(Test test) {
        return this.save(test);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateTest(Test test) {
        return this.updateById(test);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteTest(Integer id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteTests(String ids) {
        List<String> idList = StrUtil.splitTrim(ids, ",");
        return this.removeByIds(idList);
    }

    @Override
    public Test getTest(Integer id) {
        return this.getById(id);
    }

    @Override
    public List<Test> listTests(CommonRequest commonRequest, Test test) {
        QueryWrapper<Test> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件

        // 处理排序字段
        SortUtil.handleListSort(commonRequest, queryWrapper);
        return this.list(queryWrapper);
    }

    @Override
    public Map<String, Object> listPageTests(CommonRequest commonRequest, Test test) {
        Map<String, Object> resultMap = new HashMap<>(2);
        Page<Test> page = new Page<>(commonRequest.getPageNum(), commonRequest.getPageSize());
        QueryWrapper<Test> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件

        // 处理排序字段
        SortUtil.handlePageSort(commonRequest, page);
        IPage<Test> result = this.page(page, queryWrapper);
        resultMap.put("rows", result.getRecords());
        resultMap.put("total", result.getTotal());
        return resultMap;
    }
 }
